#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qcustomplot.h"
#include "dataclass.h"
#include <QFileDialog>
#include <QDebug>
//#include "stdio.h"


DataClass *dataC = new DataClass;
QVector<double> diffX(1000);

double minX,maxX,minY,maxY;

bool flag[14];


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setWindowTitle("炮车振动/姿态信息分析评估软件");
    setWindowIcon(QIcon(":/bus.png"));
    QCustomPlot *customPlot = ui->widget;//customPlot指针指向控件
    customPlot->setParent(this);//设置对象树

    //工具栏中可以设置内容
    ui->actionHome->setIcon(QIcon(":/home.png"));
    ui->actionboxSeclect->setIcon(QIcon(":/big.png"));
    ui->actiondrag->setIcon(QIcon(":/hand.png"));
    ui->toolBar->addAction(ui->actionHome);
    ui->toolBar->addSeparator();
    ui->toolBar->addAction(ui->actiondrag);
    ui->toolBar->addAction(ui->actionboxSeclect);
    addToolBar(Qt::LeftToolBarArea, ui->toolBar);//设置工具栏在左边
    ui->toolBar->setAllowedAreas(Qt::LeftToolBarArea);//后期设置：只允许工具栏左右停靠
    ui->toolBar->setFloatable(false);//默认为true
    ui->toolBar->setMovable(false);//设置移动（总开关）

    //在状态栏中添加Label
    QLabel *label = new QLabel("文件路径",this);
    ui->statusBar->addWidget(label);
    QLabel *label2 = new QLabel("时间表示",this);
    ui->statusBar->addPermanentWidget(label2);


    //功能按钮check
    ui->actionWave0->setChecked(true);ui->actionWave1->setChecked(true);
    ui->actionWave2->setChecked(true);ui->actionWave3->setChecked(true);
    ui->actionWave4->setChecked(true);ui->actionWave5->setChecked(true);
    ui->actionWave6->setChecked(true);ui->actionWave7->setChecked(true);
    ui->actionWave8->setChecked(true);ui->actionWave9->setChecked(true);
    ui->actionWaveA->setChecked(true);ui->actionWaveB->setChecked(true);
    ui->actionPitch->setChecked(true);ui->actionRoll->setChecked(true);
    ui->actiondrag->setChecked(false);
    ui->actionboxSeclect->setChecked(false);

    checkWave0();checkWave1();checkWave2();checkWave3();checkWave4();checkWave5();checkWave6();
    checkWave7();checkWave8();checkWave9();checkWaveA();checkWaveB();checkPitch();checkRoll();

    //下面代码的功能：
    //连接Wave按钮和操作：如果选择，则正常绘制数据，如果不选择，则置零该通道数据
    connect(ui->actionWave0, &QAction::triggered, this, [=](){
        checkWave0();
        findPlot();
    });
    connect(ui->actionWave1, &QAction::triggered, this, [=](){
        checkWave1();
        findPlot();
    });
    connect(ui->actionWave2, &QAction::triggered, this, [=](){
        checkWave2();
        findPlot();
    });
    connect(ui->actionWave3, &QAction::triggered, this, [=](){
        checkWave3();
        findPlot();
    });
    connect(ui->actionWave4, &QAction::triggered, this, [=](){
        checkWave4();
        findPlot();
    });
    connect(ui->actionWave5, &QAction::triggered, this, [=](){
        checkWave5();
        findPlot();
    });
    connect(ui->actionWave6, &QAction::triggered, this, [=](){
        checkWave6();
        findPlot();
    });
    connect(ui->actionWave7, &QAction::triggered, this, [=](){
        checkWave7();
        findPlot();
    });
    connect(ui->actionWave8, &QAction::triggered, this, [=](){
        checkWave8();
        findPlot();
    });
    connect(ui->actionWave9, &QAction::triggered, this, [=](){
        checkWave9();
        findPlot();
    });
    connect(ui->actionWaveA, &QAction::triggered, this, [=](){
        checkWaveA();
        findPlot();
    });
    connect(ui->actionWaveB, &QAction::triggered, this, [=](){
        checkWaveB();
        findPlot();
    });
    connect(ui->actionPitch, &QAction::triggered, this, [=](){
        checkPitch();
        findPlot();
    });
    connect(ui->actionRoll, &QAction::triggered, this, [=](){
        checkRoll();
        findPlot();
    });

    connect(ui->actionWaveform, &QAction::triggered, this, [=](){

        QString str = QFileDialog::getOpenFileName(this,"打开文件", "E:/", "(*.txt)");
        label->setText("文件路径："+str);
        label->repaint();
        dataC->txt2dataV(str);
        qDebug()<<"txt to dataVector over";
        homeWaveForm();

        qDebug()<<"i am not in getTimeStr";
        getTimeStr();

        label2->setText(beginTime);
        label2->repaint();
        qDebug()<<"change statusBar over";
    });
    connect(ui->actionHome, &QAction::triggered, this, &MainWindow::homeWaveForm);


    connect(ui->actiondrag, &QAction::triggered, this, [=](){
        if(ui->actiondrag->isChecked()){
            ui->actionboxSeclect->setChecked(false);
            customPlot->setSelectionRectMode(QCP::SelectionRectMode::srmNone);//不使能框选
            customPlot->setInteraction(QCP::iRangeDrag,true);//使能拖动
        }else{
            customPlot->setInteraction(QCP::iRangeDrag,false);//不使能拖动
        }
    });
    connect(ui->actionboxSeclect, &QAction::triggered, this, [=](){
        if(ui->actionboxSeclect->isChecked()){
            ui->actiondrag->setChecked(false);
            customPlot->setSelectionRectMode(QCP::SelectionRectMode::srmZoom);//使能框选
            customPlot->setInteraction(QCP::iRangeDrag,false);//不使能拖动
        }else{
            customPlot->setSelectionRectMode(QCP::SelectionRectMode::srmNone);//不使能框选
        }
    });


    /*设置图例个性化，美化部分*/
    //下面2行代码设置图例可见，并设置风格
    customPlot->legend->setVisible(true);
    customPlot->legend->setFont(QFont("Helvetica",9));

    //下面代码的功能：添加图线，并设置颜色
    customPlot->addGraph();
    customPlot->graph(0)->setPen(QPen(Qt::blue));
    customPlot->addGraph();
    customPlot->graph(1)->setPen(QPen(Qt::red));
    customPlot->addGraph();
    customPlot->graph(2)->setPen(QPen(Qt::yellow));
    customPlot->addGraph();
    customPlot->graph(3)->setPen(QPen(Qt::green));
    customPlot->addGraph();
    customPlot->graph(4)->setPen(QPen(Qt::black));
    customPlot->addGraph();
    customPlot->graph(5)->setPen(QPen(Qt::magenta));
    customPlot->addGraph();
    customPlot->graph(6)->setPen(QPen(Qt::lightGray));
    customPlot->addGraph();
    customPlot->graph(7)->setPen(QPen(Qt::cyan));
    customPlot->addGraph();
    customPlot->graph(8)->setPen(QPen(Qt::darkRed));
    customPlot->addGraph();
    customPlot->graph(9)->setPen(QPen(Qt::darkCyan));
    customPlot->addGraph();
    customPlot->graph(10)->setPen(QPen(Qt::darkYellow));
    customPlot->addGraph();
    customPlot->graph(11)->setPen(QPen(Qt::darkYellow));////颜色有问题
    customPlot->addGraph();//通道11为俯仰角
    customPlot->graph(12)->setPen(QPen(Qt::darkBlue));
    customPlot->addGraph();//通道12为翻滚角
    customPlot->graph(13)->setPen(QPen(Qt::darkMagenta));

    //下面6行代码设置图例中的graph名称
    customPlot->graph(0)->setName("通道0");customPlot->graph(1)->setName("通道1");
    customPlot->graph(2)->setName("通道2");customPlot->graph(3)->setName("通道3");
    customPlot->graph(4)->setName("通道4");customPlot->graph(5)->setName("通道5");
    customPlot->graph(6)->setName("通道6");customPlot->graph(7)->setName("通道7");
    customPlot->graph(8)->setName("通道8");customPlot->graph(9)->setName("通道9");
    customPlot->graph(10)->setName("通道10");customPlot->graph(11)->setName("通道11");
    customPlot->graph(12)->setName("pitch");customPlot->graph(13)->setName("roll");

    //下面4行代码设置customPlot更好看
    customPlot->xAxis2->setVisible(true);
    customPlot->xAxis2->setTickLabels(false);
    customPlot->yAxis2->setVisible(true);
    customPlot->yAxis2->setTickLabels(false);

    /*通过下面这句语句，可以屏蔽掉QCustomPlot的鼠标消息*/
    //customPlot->setAttribute(Qt::WA_TransparentForMouseEvents, true);

}

void MainWindow::getTimeStr(){
    int Year,Mou,Day,Hour,Min,Sec,Usec,Point;
    int bYear,bMou,bDay,bHour,bMin,bSec,bUsec;
    int eYear,eMou,eDay,eHour,eMin,eSec,eUsec;
    int bSecFlag=0,bMinFlag=0,bHourFlag=0,bDayFlag=0,bMouFlag=0,bYearFlag=0;
    int eUsecPP=0,eSecPP=0,eMinPP=0,eHourPP=0,eDayPP=0,eMouPP=0;

    //下面计算各个时间，并转化为int类型
    QString message = QString::fromStdString(dataC->line);
    bYear = Year = message.mid(8,4).toInt();
    bMou = Mou = message.mid(13,2).toInt();
    bDay = Day = message.mid(16,2).toInt();
    bHour = Hour = message.mid(19,2).toInt();
    bMin = Min = message.mid(22,2).toInt();
    bSec = Sec = message.mid(25,2).toInt();
    QString us_point = message.remove(message.left(28));
    bUsec = Usec = us_point.left(us_point.indexOf(":")).toInt();
    Point = us_point.remove(us_point.left(us_point.indexOf(":")+1)).toInt();

    if((16384-Point)*100%1000000>Usec){
        bUsec=bUsec+1000000-(16384-Point)*100%1000000;
        bSecFlag=1;
    }else{
        bUsec-=(16384-Point)*100%1000000;
    }
    if((16384-Point)/10000+bSecFlag>bSec){
        bSec=bSec+60-(16384-Point)/10000-bSecFlag;
        bMinFlag=1;
    }else{
        bSec-=(16384-Point)/10000;
        bSec-=bSecFlag;
    }
    if(bMinFlag>bMin){
        bMin=bMin+60-bMinFlag;
        bHourFlag=1;
    }else{
        bMin-=bMinFlag;
    }
    if(bHourFlag>bHour){
        bHour=bHour+24-bHourFlag;
        bDayFlag=1;
    }else{
        bHour-=bHourFlag;
    }
    if(bDayFlag>bDay){//这里的月份，每个月时间不一样//需要修改！！！//这里暂时用的31代替所有的月份//可以用数组
        bDay=bDay+31-bDayFlag;
        bMouFlag=1;
    }else{
        bDay-=bDayFlag;
    }
    if(bMouFlag>bMou){
        bMou=bMou+12-bMouFlag;
        bYearFlag=1;
    }else{
        bMou-=bMouFlag;
    }
    bYear-=bYearFlag;

    eYear = bYear;
    eMou = bMou;
    eDay = bDay;
    eHour = bHour;
    eMin = bMin;
    eSec = bSec;
    eUsec = bUsec;

    eUsecPP = eUsec+dataC->min_data_size*100;
    eUsec = eUsecPP%1000000;
    eSecPP = eSec+eUsecPP/1000000;
    eSec = eSecPP%60;
    eMinPP = eMin+eSecPP/60;
    eMin = eMinPP%60;
    eHourPP = eHour+eSecPP/60;
    eHour = eHourPP%24;
    eDayPP = eDay+eHourPP/24;
    eDay = eDayPP%31;//这里的月份，每个月时间不一样//需要修改！！！//这里暂时用的31代替所有的月份//可以用数组

    eMouPP = eMou+eDayPP/31;//这里的月份，每个月时间不一样//需要修改！！！//这里暂时用的31代替所有的月份//可以用数组
    eMou = eMouPP%12;
    eYear = eYear+eMouPP/12;

//        qDebug()<<Year<<"/"<<Mou<<"/"<<Day<<"="<<Hour<<":"<<Min<<":"<<Sec<<"---"<<Usec<<":"<<Point;
//        qDebug()<<bYear<<"年"<<bMou<<"月"<<bDay<<"日"<<bHour<<":"<<bMin<<":"<<bSec<<":"<<bUsec;
//        qDebug()<<eYear<<"年"<<eMou<<"月"<<eDay<<"日"<<eHour<<":"<<eMin<<":"<<eSec<<":"<<eUsec;
    char sstr[200];
    sprintf(sstr, "开始时间:%d年%d月%d日%d时%d分%d秒%dus。 结束时间:%d年%d月%d日%d时%d分%d秒%dus。 采样率10K/s。 每点间隔100us",
            bYear,bMou,bDay,bHour,bMin,bSec,bUsec,eYear,eMou,eDay,eHour,eMin,eSec,eUsec);
    beginTime = QString::fromStdString(sstr);
    qDebug()<<beginTime;
}

void MainWindow::checkWave0(){
    if(ui->actionWave0->isChecked()){
        flag[0] = true;
    }else{
        flag[0] = false;
    }
}
void MainWindow::checkWave1(){
    if(ui->actionWave1->isChecked()){
        flag[1] = true;
    }else{
        flag[1] = false;
    }
}
void MainWindow::checkWave2(){
    if(ui->actionWave2->isChecked()){
        flag[2] = true;
    }else{
        flag[2] = false;
    }
}
void MainWindow::checkWave3(){
    if(ui->actionWave3->isChecked()){
        flag[3] = true;
    }else{
        flag[3] = false;
    }
}
void MainWindow::checkWave4(){
    if(ui->actionWave4->isChecked()){
        flag[4] = true;
    }else{
        flag[4] = false;
    }
}
void MainWindow::checkWave5(){
    if(ui->actionWave5->isChecked()){
        flag[5] = true;
    }else{
        flag[5] = false;
    }
}
void MainWindow::checkWave6(){
    if(ui->actionWave6->isChecked()){
        flag[6] = true;
    }else{
        flag[6] = false;
    }
}
void MainWindow::checkWave7(){
    if(ui->actionWave7->isChecked()){
        flag[7] = true;
    }else{
        flag[7] = false;
    }
}
void MainWindow::checkWave8(){
    if(ui->actionWave8->isChecked()){
        flag[8] = true;
    }else{
        flag[8] = false;
    }
}
void MainWindow::checkWave9(){
    if(ui->actionWave9->isChecked()){
        flag[9] = true;
    }else{
        flag[9] = false;
    }
}
void MainWindow::checkWaveA(){
    if(ui->actionWaveA->isChecked()){
        flag[10] = true;
    }else{
        flag[10] = false;
    }
}
void MainWindow::checkWaveB(){
    if(ui->actionWaveB->isChecked()){
        flag[11] = true;
    }else{
        flag[11] = false;
    }
}
void MainWindow::checkPitch(){
    if(ui->actionWaveA->isChecked()){
        flag[12] = true;
    }else{
        flag[12] = false;
    }
}
void MainWindow::checkRoll(){
    if(ui->actionWaveA->isChecked()){
        flag[13] = true;
    }else{
        flag[13] = false;
    }
}

void MainWindow::mD2QVector(){
    qDebug()<<"#sizeFoot="<<dataC->plot_sizeFoot;
    for (int i=0; i<1000; i++){
        diffX[i] = i*dataC->plot_sizeFoot;
    }
    ui->widget->graph(0)->setData(dataC->indData, dataC->maxData0);
    ui->widget->graph(1)->setData(dataC->indData, dataC->maxData1);
    ui->widget->graph(2)->setData(dataC->indData, dataC->maxData2);
    ui->widget->graph(3)->setData(dataC->indData, dataC->maxData3);
    ui->widget->graph(4)->setData(dataC->indData, dataC->maxData4);
    ui->widget->graph(5)->setData(dataC->indData, dataC->maxData5);
    ui->widget->graph(6)->setData(dataC->indData, dataC->maxData6);
    ui->widget->graph(7)->setData(dataC->indData, dataC->maxData7);
    ui->widget->graph(8)->setData(dataC->indData, dataC->maxData8);
    ui->widget->graph(9)->setData(dataC->indData, dataC->maxData9);
    ui->widget->graph(10)->setData(dataC->indData, dataC->maxDataA);
    ui->widget->graph(11)->setData(dataC->indData, dataC->maxDataB);
    ui->widget->graph(12)->setData(diffX, dataC->pitchCa);
    ui->widget->graph(13)->setData(diffX, dataC->rollCa);
}

void MainWindow::findPlot(){
    dataC->findMaxMinData(int(minX), int(maxX), flag);
    mD2QVector();
    ui->widget->replot();
}

void MainWindow::homeWaveForm(){
    maxX = dataC->min_data_size;
    minX = 0;
    minY = 0;
    maxY = 4096;
    dataC->findMaxMinData(int(minX), int(maxX), flag);
    qDebug()<<"find max/min/ind over";

    mD2QVector();

    ui->widget->graph(0)->rescaleAxes();
    ui->widget->yAxis->setRange(int(minY), int(maxY));
    ui->widget->replot();
}

MainWindow::~MainWindow(){
    delete ui;
}
